﻿using Autofac;
using Microsoft.AspNetCore.Mvc;
using SimpleInOutControl.Attributes;
using SimpleInOutControl.Common;
using SimpleInOutControl.DBModels;
using SimpleInOutControl.Services;
using SimpleInOutControl.Utility;

namespace SimpleInOutControl.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UserInfoController: ControllerBase
    {
        UserInfoService service = new UserInfoService();

        protected CommunicationService _communicationService;
        public UserInfoController(CommunicationService communicationService)
        {
            this._communicationService = communicationService;
        }

        [HttpPost]
        public ResponseResult<bool> AddData(UserInfo dto)
        {
            var ddd = this._communicationService.GetDeviceInfos();
            //var ddd = this._componentContext.Resolve<ICommunicationService>();
            var result = service.AddData(dto);
            return new ResponseResult<bool>()
            {
                Code = result ? 0 : 1,
                Message = result ? "成功" : "失败",
                Result = result
            };
        }

        [HttpPut]
        public ResponseResult<bool> UpdateData(UserInfo dto)
        {
            var result = service.UpdateData(dto);
            return new ResponseResult<bool>()
            {
                Code = result ? 0 : 1,
                Message = result ? "成功" : "失败",
                Result = result
            };
        }

        [HttpDelete]
        public ResponseResult<bool> DeleteData(int id)
        {
            var result = service.DeleteData(id);
            return new ResponseResult<bool>()
            {
                Code = result ? 0 : 1,
                Message = result ? "成功" : "失败",
                Result = result
            };
        }

        [HttpGet]
        public ResponseResult<UserInfo> GetData(int id)
        {
            var result = service.GetData(id);
            return new ResponseResult<UserInfo>()
            {
                Code = result != null ? 0 : 1,
                Message = result != null ? "成功" : "失败",
                Result = result 
            };
        }

        [HttpPost]
        public ResponseResult<PagingData<UserInfo>> GetPageData(PagingRequest<UserInfo> dto)
        {
            UserInfoService service = new UserInfoService();
            var result = service.GetPageData(dto);
            return new ResponseResult<PagingData<UserInfo>>()
            {
                Code = result != null ? 0 : 1,
                Message = result != null ? "成功" : "失败",
                Result = result
            };
        }

        [HttpPost]
        [NoLogin]
        public ResponseResult<string> Login(UserInfo dto)
        {
            var user = service.Login(dto);

            if (user != null)
            {
                //生成token
                var token = JwtUtils.CreateToken(user);

                return new ResponseResult<string>()
                {
                    Code = (!string.IsNullOrEmpty(token)) ? 0 : 1,
                    Message = (!string.IsNullOrEmpty(token)) ? "成功" : "失败",
                    Result = token
                };
            }
            else
            {
                return new ResponseResult<string>()
                {
                    Code = 1,
                    Message = "用户名或密码错误",
                    Result= "用户名或密码错误"
                };
            }
        }
    }
}
